<?php
/**
 * In this file the internal class '\BF\PasswordDeriveBytes' is defined.
 *
 * @author     Kado <ulfikado@gmail.com>
 * @category   Beluga PHP5.3+ Framework
 * @package    BF\Core
 * @since      2015-01-17 14:29
 * @subpackage Security
 * @version    0.1
 * @ignore
 */

namespace BF
{

   /**
    * @ignore
    */
   class PasswordDeriveBytes
   {

      # <editor-fold defaultstate="collapsed" desc=" - - -   P R I V A T E   F I E L D S   - - - - - - - - - - - - - - - - - - - - - -">

      private $bytes;
      private $string;
      private $len;

      # </editor-fold>

      # <editor-fold defaultstate="collapsed" desc=" - - >   P U B L I C   C O N S T R U C T O R   - - - - - - - - - - - - - - - - - -">

      /**
       * @param $string
       * @param $bytes
       */
      public function __construct( $string, $bytes )
      {
         $this->bytes  = $bytes;
         $this->string = $string;
         $this->len    = \BF\strlen( $string );
      }

      # </editor-fold>

      # <editor-fold defaultstate="collapsed" desc=" - - -   P U B L I C   M E T H O D S   - - - - - - - - - - - - - - - - - - - - - -">

      /**
       * @param $count
       * @return string
       */
      public function getBytes($count)
      {
         if ( $this->len > $count )
         {
            return \BF\substr ( $this->string, 0, $count );
         }
         if ( $this->len == $count )
         {
            return $this->string;
         }
         $res = $this->string;
         $l = $this->len;
         $i = 0;
         while ( $l < $count )
         {
            $res .= $this->bytes[$i];
            $l = \BF\strlen( $res );
         }
         if ( $l > $count )
         {
            return \BF\substr( $res, 0, $count );
         }
         return $res;
      }

      # </editor-fold>

   }

}

